import 'package:flutter/material.dart';

class PaperAppBar extends StatelessWidget implements PreferredSizeWidget {
  final VoidCallback? onBack;
  final VoidCallback? onRevocation;
  final VoidCallback onClear;

  const PaperAppBar(
      {super.key,
      required this.onClear,
      required this.onBack,
      required this.onRevocation});

  @override
  Widget build(BuildContext context) {
    return AppBar(
      iconTheme: const IconThemeData(color: Colors.black),
      centerTitle: true,
      title: const Text(
        "画板绘制",
        style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
      ),
      elevation: 0,
      backgroundColor: Colors.white,
      leadingWidth: 100,
      leading: BackUpButtons(onBack: onBack, onRevocation: onRevocation),
      actions: [
        IconButton(onPressed: onClear, icon: const Icon(Icons.delete_outlined))
      ],
    );
  }

  @override
  Size get preferredSize => const Size.fromHeight(kToolbarHeight);
}

/// 回退与撤销按钮
class BackUpButtons extends StatelessWidget {
  final VoidCallback? onBack;
  final VoidCallback? onRevocation;

  const BackUpButtons(
      {super.key, required this.onBack, required this.onRevocation});

  @override
  Widget build(BuildContext context) {
    const BoxConstraints cts = BoxConstraints(maxHeight: 32, minWidth: 32);
    Color backColor = onBack == null ? Colors.grey : Colors.black;
    Color revocationColor = onRevocation == null ? Colors.grey : Colors.black;
    return Center(
      child: Wrap(
        children: [
          Transform.scale(
            scaleX: -1,
            child: IconButton(
              splashRadius: 10,
              constraints: cts,
              onPressed: onBack,
              icon: Icon(
                Icons.next_plan_outlined,
                color: backColor,
              ),
            ),
          ),
          IconButton(
              splashRadius: 10,
              constraints: cts,
              onPressed: onRevocation,
              icon: Icon(
                Icons.next_plan_outlined,
                color: revocationColor,
              ))
        ],
      ),
    );
  }
}
